package org.example.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.dao.TestMapper;
import org.example.domain.Test;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: tang
 * @Description:
 * @Date 2025/6/5 23:23
 */
@Slf4j
@Service
public class TestServiceImpl implements TestService{

    @Resource
    private TestMapper testMapper;

    @DSTransactional
    @Override
    public List<Test> testPage() {
        Page<Test> page = new Page<>(1, 10);
        IPage<Test> iPage = testMapper.testPage(page);
        return iPage.getRecords();
    }

    @Override
    public Test test() {
        return testMapper.test();
    }

    @Override
    public Test test01() {
        return testMapper.test01();
    }

    @DSTransactional
    @Override
    public Test test02() {
        testMapper.deleteById(2);
        return testMapper.test02();
    }

    @DSTransactional
    @Override
    public void test03() {
        // 动态数据源
        System.out.println(test02());
        System.out.println(test());
    }

    @DSTransactional(rollbackFor = Exception.class)
//    @Transactional(rollbackFor = Exception.class)
    @Override
    public void test04() throws Exception{
        testMapper.test0041();
        testMapper.test0042();
    }
}
